﻿#include <QDebug>

#include "QGeoTileFetcherCustom.h"
#include "MapEngine.h"
#include "UrlFactory.h"
#include "QGeoTiledMapReplyCustom.h"

QGeoTileFetcherCustom::QGeoTileFetcherCustom(QGeoMappingManagerEngine *parent)
    : QGeoTileFetcher(parent)
    , _networkManager(new QNetworkAccessManager(this))
{
    //-- Check internet status every 30 seconds or so
    connect(&_timer, &QTimer::timeout, this, &QGeoTileFetcherCustom::timeout);
    _timer.setSingleShot(false);
    _timer.start(30000);
}

void QGeoTileFetcherCustom::timeout()
{
    getMapEngine()->testInternet();
}

/**
 * @brief 这个方法应该是由框架调用的吧？
 * @param spec
 * @return
 */
QGeoTiledMapReply *QGeoTileFetcherCustom::getTileImage(const QGeoTileSpec &spec)
{
    qDebug() << "getTileImage" << spec.mapId() << spec.x() << spec.y() << spec.zoom();
    //-- Build URL
    QNetworkRequest request = getMapEngine()->urlFactory()->getTileUrl(spec.mapId(), spec.x(), spec.y(), spec.zoom(), _networkManager);
    if (!request.url().isEmpty()) {
        return new QGeoTiledMapReplyCustom(_networkManager, request, spec);
    }
    else {
        return nullptr;
    }
}
